﻿
using Models;
using System;
using System.Collections.Generic;
using VirtualGameDAL;

namespace VirtualGameBLL
{
    public static partial class RoleManager
    {
        private static Role AddRole(Role role)
        {
            if (role.Wpn == null) {
                role.Wpn = WeaponManager.GetDefaultWeapon();
            }
            return RoleService.AddRole(role);
        }

        public static void DeleteRoleById(int id)
        {
            RoleService.DeleteRolerById(id);
        }


        public static List<Role> GetAllFriends() {
            List<Role> friends = new List<Role>(RoleService.GetAllFriends());
            friends.Sort(delegate (Role x, Role y) { return y.Speed.CompareTo(x.Speed); });
            return friends;
        }

        public static List<Role>  GetAllEnemies()
        {
            List<Role> enemies = new List<Role>(RoleService.GetAllEnemies());
            enemies.Sort(delegate (Role x, Role y) { return y.Speed.CompareTo(x.Speed); });
            return enemies;
        }

        public static void Save(List<Role> roles)
        {
            foreach (Role role in roles) {
                RoleService.ModifyRole(role);
            }
        }

        public static double Atk(Role atker,Role defender)
        {
            int hurt = GetAttack(atker) - GetDefense(defender);
            defender.CurrentLife -= hurt;

            if (defender.CurrentLife <= 0)
                defender.CurrentLife = 0;
            return 1.0*defender.CurrentLife/defender.OriginalLife;
        }

        public static int GetAttack(Role role)
        {
            if (role.Type == 0 || role.Type == 2) {
                return (int)((role.Damage * 2 + role.Brains * 4 + role.Wpn.Atk) * GetSuperAttack(role.Wpn.Luck));
                //魔法师攻击力=力量*2+智力*4+武器的攻击力
            }
            return (int)((role.Damage * 4.5 + role.Brains * 1.2 + role.Wpn.Atk) * GetSuperAttack(role.Wpn.Luck));
            // 战士攻击力 = 力量 * 4.5 + 智力 * 1.2 + 武器的攻击力
        } 
        
        public static int GetDefense(Role role)
        {
            if (role.Type == 0 || role.Type == 2) {
                return (int)(role.Agile * 3.6 + role.Damage * 1.5 + role.Brains * 2 + role.Wpn.Defense);
                //魔法师防御力=敏捷*3.6+力量*1.5+智力*2+武器的防御力
             }
            return (int)(role.Agile * 8 + role.Damage * 2.5 + role.Brains * 1 + role.Wpn.Defense);
            // 战士防御力 = 敏捷 * 8 + 力量 * 2.5 + 智力 * 1 + 武器的防御力
        }

        public static double GetSuperAttack(int luck)
        {
            Random rd = new Random();
            int probability = rd.Next(0, 101) + luck * 10;   // (生成0~101之间的随机数，不包括101)
            if (probability < 50)
                return 1;
            else
                return 1.5;
        }
    }
}
/*
法师为x0    0||2
战士为x1    1||3

己方为0x    0||1
敌方为1x    2||3

    role                     type
----------------------------------
magician1           00    0
warrior1              01    1
necromancer1    10    2
minotaur1           11    3
*/
